package org.healthy.serviceimpl;

import com.alibaba.fastjson.JSON;
import org.apache.noggit.JSONUtil;
import org.healthy.base.BaseService;
import org.healthy.base.constant.RedisKeyConst;
import org.healthy.constant.CommonConst;
import org.healthy.dao.UserMapper;
import org.healthy.model.user.User;
import org.healthy.service.IUserService;
import org.healthy.utils.JsonUtil;
import org.healthy.utils.pub.tool.StringUtils;
import org.healthy.utils.redis.CommRedisOperator;

import javax.annotation.Resource;

/**
 * Created by qingshengzheng on 17/10/11.
 */
public class UserServiceImpl extends BaseService implements IUserService {
    private CommRedisOperator redisoper;
    @Resource
    public UserMapper userMapper;

    /**
     * 发送手机验证码(暂时不实用)
     *
     * @param mobile
     * @return
     */
    public String sendMobileCode(String mobile) {
        return null;
    }

    /**
     * 注册用户
     *
     * @param user
     * @return
     */
    public User registerUser(User user) {
        return null;
    }

    public User registerByOpenid(String openid, String type) {
        User user = new User();
        if (CommonConst.TYPE_QQ.equals(type)) {
            user.setQq(openid);

        } else if (CommonConst.TYPE_WEIBO.equals(type)) {
            user.setWeibo(openid);

        } else if (CommonConst.TYPE_WEIXIN.equals(type)) {
            user.setWeixin(openid);
        }
        User lastUser = registerUser(user);
        return lastUser;

    }

    /**用户
     * @param user
     */


    public void update(User user) {
        userMapper.modfiyUser(user);
        String userKey=redisoper.format(RedisKeyConst.CACHE_USER,user.getUid());
        redisoper.set(userKey, JSON.toJSONString(user),0);
    }

    /**
     * 第三方用户登录
     *
     * @param openid
     * @param type
     * @return
     */
    public User userLogin(String openid, String type) {
        return null;
    }

    /**
     * 判断第三方帐号是否存在
     *
     * @param openid
     * @param type
     * @return
     */
    public Boolean isThridExist(String openid, String type) {
        String key = redisoper.format(RedisKeyConst.USER_OPENID_EXISTS, openid, type);
        if (redisoper.exists(key, 0))
            return true;
        else return false;
    }


    /**
     * 根据手机验证码登录
     *
     * @param mobile
     * @param code
     * @return
     */
    public User userLoginByCode(String mobile, String code) {
        return null;
    }

    /**
     * 判断用户是否存在
     *
     * @param mobile
     * @return
     */
    public Boolean isUserExist(String mobile) {
        return false;
    }

    /**
     * 判断用户是否存在
     *
     * @param uid
     * @return
     */
    public Boolean isUserExist(Long uid) {
        String key = redisoper.format(RedisKeyConst.USER_OPENID_EXISTS);
        if (redisoper.exists(key, 0))
            return true;
        else return false;
    }


    /**
     * 重置密码暂时未使用
     * @param mobile
     * @param deftPassWord
     */
    public void resetPassWord(String mobile, String deftPassWord) {


    }

    /**
     * 根据手机号码获取用户详情
     * @param mobile
     * @return
     */
    public User userDetailByMobile(String mobile) {
        return null;
    }

    public User userDetailByUid(Long uid)
    {
        User user=null;
        String key=redisoper.format(RedisKeyConst.CACHE_USER,uid);
        String json= redisoper.get(key,0);
        if(!StringUtils.isNull(json))
            user= (User) JsonUtil.fromJson(json,User.class);
        else
            user=userMapper.queryUserByUid(uid);
        return null;
    }
}
